package com.lhkj.ct.admin.modules.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.admin.model.dto.EmpPageQuery;
import com.lhkj.ct.meta.admin.model.entity.TblDepartment;
import com.lhkj.ct.meta.admin.model.vo.EmpPageVo;
import com.lhkj.ct.meta.admin.service.SysDeptService;
import com.lhkj.ct.meta.admin.service.SysDictDataService;
import com.lhkj.ct.meta.admin.service.SysEmployeeService;
import com.lhkj.ct.meta.admin.service.SysOrganService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleConfigService;
import com.lhkj.ct.meta.modules.patient.model.dto.PatientQuery;
import com.lhkj.ct.meta.modules.patient.model.vo.PatientBasicVo;
import com.lhkj.ct.meta.modules.patient.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *      TODO 通用接口
 * </p>
 *
 * @author jobob
 * @since 2023-08-23
 */
@RestController
@RequestMapping("api/versatile")
@Api(tags = "0.通用接口")
@ApiSupport(order = 0)
@Validated
public class VersatileController {

    @Resource
    private SysDictDataService dictDataService;

    @Resource
    private SysOrganService sysOrganService;

    @Resource
    private SysDeptService sysDeptService;

    @Resource
    private SysEmployeeService employeeService;

    @Resource
    private PatientService patientService;

    @Resource
    private ScaleConfigService scaleConfigService;


    @GetMapping("dictionary")
    @ApiOperation(value = "字典查询", notes = "统一的字典查询接口, dictType为字典类型编码(如多个则以‘,’分割)，" +
            "性别：user_gender，" +
            "证件类型：user_ident_type，" +
            "民族：user_nation," +
            "学历：user_education," +
            "婚姻状况：user_marriage")
    public ActionResult<Map<String, List<Option<String>>>> list(@NotBlank(message = "字典编码不能为空") String dictType) {
        Map<String, List<Option<String>>> map = new HashMap<>();
        String[] dictTypes = dictType.split(",");
        for (String type : dictTypes) {
            map.put(type, dictDataService.selectDictDataByType(type));
        }
        return ActionResult.success(map);
    }

    @GetMapping("options/org")
    @ApiOperation(value = "机构树级下拉列表", notes = "action类型 ==> organ:机构列表 dept:科室列表（params：orgId为机构id,非必传）, doctor:医生列表（分页, params：empName为医生名称）")
    public ActionResult<List<Option<String>>> org() {
        return ActionResult.success(sysOrganService.listOptions());
    }

    @GetMapping("options/dept")
    @ApiOperation(value = "科室下拉列表", notes = "action类型 ==> organ:机构列表 dept:科室列表（params：orgId为机构id,非必传）, doctor:医生列表（分页, params：empName为医生名称）")
    public ActionResult<List<Option<String>>> options(TblDepartment department) {
        return ActionResult.success(sysDeptService.listOptions(department.getOrganId()));
    }

    @GetMapping("options/sysUser")
    @ApiOperation(value = "系统用户（医生）下拉列表", notes = "deptId:按科室搜索 empName:按姓名搜索")
    public ActionResult<IPage<EmpPageVo>> options(EmpPageQuery pageQuery) {
        return ActionResult.success(employeeService.selectEmpOptions(pageQuery));
    }

    @ApiOperation(value = "档案列表")
    @GetMapping("patients")
    public ActionResult<IPage<PatientBasicVo>> selectPatUnitPage(PatientQuery patientQuery) {
        return ActionResult.success(patientService.getBasicUserPage(patientQuery));
    }

    @ApiOperation(value = "量表列表")
    @GetMapping("scaleOptions")
    public ActionResult<IPage<Option<String>>> scaleOptions(PageQuery pageQuery) {
        return ActionResult.success(scaleConfigService.selectScaleOptions(pageQuery));
    }
}
